/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package bd.org.apache.commons.math.special;

import bd.org.apache.commons.math.util.ContinuedFraction;
import bd.org.apache.commons.math.util.FastMath;

/**
 * This is a utility class that provides computation methods related to the
 * Beta family of functions.
 *
 * @version $Id: Beta.java 1244107 2012-02-14 16:17:55Z erans $
 */
public class Beta {

    /**
     * Maximum allowed numerical error.
     */
    private static final double DEFAULT_EPSILON = 10e-15;

    /**
     * Default constructor.  Prohibit instantiation.
     */
    private Beta() {}

    /**
     * Returns the
     * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
     * regularized beta function</a> I(x, a, b).
     *
     * @param x Value.
     * @param a Parameter {@code a}.
     * @param b Parameter {@code b}.
     * @return the regularized beta function I(x, a, b).
     */
    public static double regularizedBeta(double x, double a, double b) {

        return regularizedBeta(x, a, b, DEFAULT_EPSILON, Integer.MAX_VALUE);
    }


    /**
     * Returns the
     * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
     * regularized beta function</a> I(x, a, b).
     *
     * @param x Value.
     * @param a Parameter {@code a}.
     * @param b Parameter {@code b}.
     * @param epsilon When the absolute value of the nth item in the
     * series is less than epsilon the approximation ceases to calculate
     * further elements in the series.
     * @return the regularized beta function I(x, a, b)
     */
    public static double regularizedBeta(double x, double a, double b, double epsilon) {

        return regularizedBeta(x, a, b, epsilon, Integer.MAX_VALUE);
    }


    /**
     * Returns the regularized beta function I(x, a, b).
     *
     * @param x the value.
     * @param a Parameter {@code a}.
     * @param b Parameter {@code b}.
     * @param maxIterations Maximum number of "iterations" to complete.
     * @return the regularized beta function I(x, a, b)
     */
    public static double regularizedBeta(double x, double a, double b, int maxIterations) {

        return regularizedBeta(x, a, b, DEFAULT_EPSILON, maxIterations);
    }


    /**
     * Returns the regularized beta function I(x, a, b).
     * The implementation of this method is based on:
     * <ul>
     * <li>
     * <a href="http://mathworld.wolfram.com/RegularizedBetaFunction.html">
     * Regularized Beta Function</a>.</li>
     * <li>
     * <a href="http://functions.wolfram.com/06.21.10.0001.01">
     * Regularized Beta Function</a>.</li>
     * </ul>
     *
     * @param x the value.
     * @param a Parameter {@code a}.
     * @param b Parameter {@code b}.
     * @param epsilon When the absolute value of the nth item in the
     * series is less than epsilon the approximation ceases to calculate
     * further elements in the series.
     * @param maxIterations Maximum number of "iterations" to complete.
     * @return the regularized beta function I(x, a, b)
     */
    public static double regularizedBeta(double x, final double a, final double b, double epsilon,
            int maxIterations) {

        double ret;

        if (Double.isNaN(x) || Double.isNaN(a) || Double.isNaN(b) || (x < 0) || (x > 1)
                || (a <= 0.0) || (b <= 0.0)) {
            ret = Double.NaN;
        } else if (x > (a + 1.0) / (a + b + 2.0)) {
            ret = 1.0 - regularizedBeta(1.0 - x, b, a, epsilon, maxIterations);
        } else {

            ContinuedFraction fraction = new ContinuedFraction() {

                @Override
                protected double getB(int n, double x) {

                    double ret;
                    double m;

                    if (n % 2 == 0) {    // even

                        m   = n / 2.0;
                        ret = (m * (b - m) * x) / ((a + (2 * m) - 1) * (a + (2 * m)));

                    } else {

                        m   = (n - 1.0) / 2.0;
                        ret = -((a + m) * (a + b + m) * x) / ((a + (2 * m)) * (a + (2 * m) + 1.0));
                    }

                    return ret;
                }

                @Override
                protected double getA(int n, double x) {

                    return 1.0;
                }
            };

            ret = FastMath.exp((a * FastMath.log(x)) + (b * FastMath.log(1.0 - x))
                               - FastMath.log(a) - logBeta(a, b, epsilon, maxIterations)) * 1.0
                                   / fraction.evaluate(x, epsilon, maxIterations);
        }

        return ret;
    }


    /**
     * Returns the natural logarithm of the beta function B(a, b).
     *
     * @param a Parameter {@code a}.
     * @param b Parameter {@code b}.
     * @return log(B(a, b)).
     */
    public static double logBeta(double a, double b) {

        return logBeta(a, b, DEFAULT_EPSILON, Integer.MAX_VALUE);
    }


    /**
     * Returns the natural logarithm of the beta function B(a, b).
     * The implementation of this method is based on:
     * <ul>
     * <li><a href="http://mathworld.wolfram.com/BetaFunction.html">
     * Beta Function</a>, equation (1).</li>
     * </ul>
     *
     * @param a Parameter {@code a}.
     * @param b Parameter {@code b}.
     * @param epsilon When the absolute value of the nth item in the
     * series is less than epsilon the approximation ceases to calculate
     * further elements in the series.
     * @param maxIterations Maximum number of "iterations" to complete.
     * @return log(B(a, b)).
     */
    public static double logBeta(double a, double b, double epsilon, int maxIterations) {

        double ret;

        if (Double.isNaN(a) || Double.isNaN(b) || (a <= 0.0) || (b <= 0.0)) {
            ret = Double.NaN;
        } else {
            ret = Gamma.logGamma(a) + Gamma.logGamma(b) - Gamma.logGamma(a + b);
        }

        return ret;
    }
}
